<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author lzw
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'operating:followrecord:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.userrName }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsOperatingFollowrecordList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { followrecordDelete, followrecordListData } from '/@/api/operating/followrecord';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  const { t } = useI18n('operating.followrecord');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('员工跟进情况表管理(仅记录有效跟进)'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('负责人名称'),
        field: 'userrName',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('负责人名称'),
      dataIndex: 'userrName',
      key: 'a.userr_name',
      sorter: true,
      width: 100,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('年份'),
      dataIndex: 'years',
      key: 'a.years',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('年度总跟进量'),
      dataIndex: 'yearsNumber',
      key: 'a.years_number',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('当日跟进量'),
      dataIndex: 'dayNumber',
      key: 'a.day_number',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('1月跟进量'),
      dataIndex: 'one',
      key: 'a.one',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('2月跟进量'),
      dataIndex: 'two',
      key: 'a.two',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('3月跟进量'),
      dataIndex: 'three',
      key: 'a.three',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('4月跟进量'),
      dataIndex: 'four',
      key: 'a.four',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('5月跟进量'),
      dataIndex: 'five',
      key: 'a.five',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('6月跟进量'),
      dataIndex: 'six',
      key: 'a.six',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('7月跟进量'),
      dataIndex: 'seven',
      key: 'a.seven',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('8月跟进量'),
      dataIndex: 'eight',
      key: 'a.eight',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('9月跟进量'),
      dataIndex: 'nine',
      key: 'a.nine',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('10月跟进量'),
      dataIndex: 'ten',
      key: 'a.ten',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('11月跟进量'),
      dataIndex: 'eleven',
      key: 'a.eleven',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('12月跟进量'),
      dataIndex: 'twelve',
      key: 'a.twelve',
      sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑员工跟进情况表'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'operating:followrecord:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    api: followrecordListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await followrecordDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }
</script>
